package com.example.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Random;

import com.example.fw.ApplicationManager;
import com.thoughtworks.xstream.XStream;

import static com.example.tests.TestBase.generateRandomString;
import static com.example.tests.TestBase.generateRandomBirthDate;

//import java.util.Properties;

//import com.example.fw.ApplicationManager;

public class ContactDataGenerator {
	
	
	//private static List<String> groupsList;

	public static void main(String[] args) throws IOException {
		
		
		if (args.length < 3) {
			System.out.println("Please specify parameters: <amount of test data> <file> <format>");
			return;
		}
		int amount = Integer.parseInt(args[0]);
		File file = new File (args[1]);
		String format = args[2];
		
		if (file.exists()) {
			System.out.println("File exists, please remove it manually: " + file);
		}
		
		List<ContactData> contacts = generateRandomContacts(amount);
		if ("csv".equals(format)) {
			saveContactsToCsvFile(contacts, file);
		} else if ("xml".equals(format)) {
			saveContactsToXmlFile(contacts, file);
		} else {
			System.out.println("Unknown format: " + format);
			return;
		}
	}

	private static void saveContactsToXmlFile(List<ContactData> contacts,
			File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		String xml = xstream.toXML(contacts);
		FileWriter writer = new FileWriter(file);
		writer.write(xml);
		writer.close();
	}
	
	public static List<ContactData> loadContactsFromXmlFile(File file) throws IOException {
		XStream xstream = new XStream();
		xstream.alias("contact", ContactData.class);
		return (List<ContactData>) xstream.fromXML(file);
	}

	private static void saveContactsToCsvFile(List<ContactData> contacts,
			File file) throws IOException {
		FileWriter writer = new FileWriter(file);
		for (ContactData contact : contacts) {
			writer.write(contact.getFirstName()+ "," + 
						 contact.getLastName()+ "," +
						 contact.getAddress()+ "," +
						 contact.getHomePhone()+ "," +
						 contact.getMobilePhone()+ "," +
						 contact.getWorkPhone()+ "," +
						 contact.getEmail1()+ "," +
						 contact.getEmail2()+ "," +
						 contact.getbDay()+ "," +
						 contact.getbMonth()+ "," +
						 contact.getbYear()+ "," +
						 contact.getContactGroup()+ "," +
						 contact.getAddress2()+ "," +
						 contact.getPhone2()+ "," +
						 contact.getContactGroup()+ "," +
						 "!" + "\n");	
		}
		
		writer.close();
	}
	
	public static List<ContactData> loadContactsFromCsvFile(File file) throws IOException {
		FileReader reader = new FileReader(file);
		List<ContactData> list = new ArrayList<ContactData>();
		BufferedReader bufferedReader = new BufferedReader(reader);
		String line = bufferedReader.readLine();
		while (line != null) {
			String[] part = line.split(",");
			ContactData contact = new ContactData()
				.withFirstName(part[0])
				.withLastName(part[1])
				.withAddress(part[2])
				.withHomePhone(part[3])
				.withMobilePhone(part[4])
				.withWorkPhone(part[5])
				.withEmail1(part[6])
				.withEmail2(part[7])
				.withContactGroup(part[13])			
				.withBDay(part[8])
				.withBMonth(part[9])
				.withBYear(part[10])
				.withAddress2(part[11])
				.withPhone2(part[12]);
			list.add(contact);
			line = bufferedReader.readLine();
		}
		bufferedReader.close();
		return list;
	}

	public static List<ContactData> generateRandomContacts(int amount) throws IOException {
		List<ContactData> list = new ArrayList<ContactData>();
		for (int i = 0; i < amount; i++) {
			 String[] birthDate = generateRandomBirthDate();
			ContactData contact = new ContactData()
				.withFirstName(generateRandomString())
				.withLastName(generateRandomString())
				.withAddress(generateRandomString())			
				.withAddress2(generateRandomString())
				.withEmail1(generateRandomString())
				.withEmail2(generateRandomString())
				.withHomePhone(generateRandomString())
				.withMobilePhone(generateRandomString())
				.withPhone2(generateRandomString())
				.withWorkPhone(generateRandomString())
				.withContactGroup(generateContactGroup())				
				.withBDay(birthDate[0])
				.withBMonth(birthDate[1])
				.withBYear(birthDate[2]);
			list.add(contact);
		}
		return list;
	}
	
	public static String generateContactGroup() throws IOException, IOException{
		Properties properties = new Properties();
		properties.load(new FileReader(new File("application.properties")));
		ApplicationManager app = new ApplicationManager(properties);
		Random rnd = new Random();
		List<GroupData> groups = app.getHibernateHelper().listGroups();
		List<String> groupsList = new ArrayList<String>();
		for (GroupData group : groups) {
			groupsList.add(group.getName());
		}
		int index = rnd.nextInt(groupsList.size());
		return groupsList.get(index);
	}
	
}
